﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Canteen.Core.Models
{
    public class PageList<T>
    {
        public PageList(List<T> List, int Current, int PageSize, int TotalCount)
        {
            this.List = List;
            this.Current = Current;
            this.PageSize = PageSize;
            Total = TotalCount;
            PageTotal = (int)Math.Ceiling((double)TotalCount / PageSize);
        }

        /// <summary>
        /// 当前页码
        /// </summary>
        public int Current { get; set; }

        /// <summary>
        /// 总记录数
        /// </summary>
        public int Total { get; set; }

        /// <summary>
        /// 每页记录数
        /// </summary>
        public int PageSize { get; set; }

        /// <summary>
        /// 总页数
        /// </summary>
        public int PageTotal { get; set; }

        /// <summary>
        /// 分页数据
        /// </summary>
        public List<T> List { get; set; }

        public static async Task<PageList<T>> CreateAsync(IQueryable<T> source, int Current, int PageSize)
        {
            var items = source.Skip((Current - 1) * PageSize).Take(PageSize).ToList();
            return await Task.FromResult(new PageList<T>(items, Current, PageSize, source.Count()));
        }
    }
}